<?php session_start(); 
include ("../function.php");
include "../dbconnect.php";
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>IQ TEST</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="<?php echo $base_url;?>css/bootstrap.css" media="screen">
    <link rel="stylesheet" href="<?php echo $base_url;?>css/bootswatch.min.css">
	<link rel="stylesheet" href="<?php echo $base_url;?>css/form_validation.css">
	<link rel="stylesheet" href="<?php echo $base_url;?>js/jquery.countdown.package-2.0.1/jquery.countdown.css">
    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="<?php echo $base_url;?>js/html5shiv.js"></script>
      <script src="<?php echo $base_url;?>s/respond.min.js"></script>
    <![endif]-->
    <style type="text/css">

	#defaultCountdown { width: 240px; height: 45px; }
	
	.highlight-warning {
	    color: yellow;
	}
	
	.highlight-timeout {
	    color: #f00;
	}
	.warning {
	    background-color: yellow;
	}
	.timeout {
	    background-color: #f00;
	}

	</style>
	<script src="<?php echo $base_url;?>js/jquery-1.10.2.min.js"></script>


	<script src="<?php echo $base_url;?>js/jquery.validate.min.js" type="text/javascript"></script>
	<script src="<?php echo $base_url;?>js/bootstrap.min.js"></script>
    <script src="<?php echo $base_url;?>js/bootswatch.js"></script>
    <script src="<?php echo $base_url;?>js/jquery.countdown.package-2.0.1/jquery.plugin.js"></script>
    <script src="<?php echo $base_url;?>js/jquery.countdown.package-2.0.1/jquery.countdown.js"></script>
    
    
    <script src="<?php echo $base_url;?>js/floating-1.12.js"></script>
    <script type="text/javascript">
	$(document).ready(function() { 
		
		$("#infoForm").validate({ }); 
	}); 
	</script> 
	    <script>
		/*$(function () {
			$('#defaultCountdown').countdown({until: 60*60, format: 'MS'});	
		});*/
		$(function () {
			$('#floatdiv').countdown({until: 60*60, format: 'MS',onExpiry: liftOff,onTick: highlightLast});	
		});
		
		function highlightLast(periods) { 
			/*if ($.countdown.periodsToSeconds(periods) === 10) {  
				$(this).addClass('highlight-warning');
				//$("#bodyall").addClass('warning');
			} */
			
			if ($.countdown.periodsToSeconds(periods) === 60) { 
				$(this).addClass('highlight-timeout');
				//$("#bodyall").addClass('timeout');
			} 
		} 
		
		function liftOff() { 
			
			$('input').removeClass('required');
			$( "#infoForm" ).submit();
		} 
	
	</script>
  </head>
<body id="bodyall">
<?php include ("../menu.php"); ?>
<?php 
	
	$student_id = $_SESSION['student_id'];
	$id_excercise = (isset($_GET['ide']))?$_GET['ide']:0;
	$times = (isset($_GET['t']))?$_GET['t']:1;
	$excercise_info = get_info_excercise($id_excercise);
	$excercise_title = $excercise_info['title'];
	$limit_times = $excercise_info['limit_times'];
	
	$can_do = checktimes_by_excercise_and_student_id($student_id,$id_excercise,$times);
?>
<?php if($can_do == 1){?>
<div id="floatdiv" style="  
    position:absolute;  
    width:150px;height:90px;top:10px;right:10px;  
    padding:16px;background:#FFFFFF;  
    border:2px solid #2266AA;  
    z-index:100"> </div> 
<?php }?>
    <div class="container">  
      <div>
        <div class="row">
          <div class="col-lg-12">
            <div class="page-header">
              <h2 id="forms">แบบทดสอบIQ : <?php echo $excercise_title;?> เวลา <?php echo $limit_times;?> นาที</h2>
              
              
            </div>
          </div>
        </div>
        <div class="row">
            <div class="well bs-component">
            <form class="form-horizontal" id="infoForm" method="post" action="<?php echo $base_url;?>excercise/result.php">
            <input type="hidden" name="id_excercise" id="id_excercise" value="<?php echo $id_excercise;?>" />
			<input type="hidden" name="times" id="times" value="<?php echo $times;?>" />
            <input type="hidden" name="student_id" id="student_id" value="<?php echo $student_id;?>" />
            <?php 
            
            if($student_id)
            {
            	if($id_excercise > 0)
            	{
            		if($can_do == 1)
            		{
            			$order = ($times > 1)? 'RAND()': "id_excercise ASC";

            			//get excercise
            			$str = "";
            			$ans = array();
            			$sql = "SELECT * FROM question WHERE id_excercise=".$id_excercise." AND status=1 ORDER BY ".$order;
            			$result = mysql_query($sql, $link);
            			mysql_query("SET NAMES UTF8");
            			$numrows = mysql_num_rows($result);
            			if($numrows > 0)
            			{
            				$num=1;
            				while ($arr = mysql_fetch_array($result))
            				{
            					$id_question = $arr['id_question'];
            					$question = $base_url."upload/excercise/".$arr['question'];
            					$str .= '<div class="panel panel-default">';
            			
            					//question
            					$str .= '<div class="panel-body">ข้อ '.$num.') <img src="'.$question.'" style="float: right; width:100%;margin-left:2rem"></div>';
            			
            					//answer
            					$ans = get_ans_by_question_id($id_question);
            					$str .= '<div class="panel-footer">';
            					for($a=1;$a<=$ans;$a++)
            					{
            					$str .= '<p>คำตอบที่ '.$a.') <input type="text" name="question'.$id_question.'[]" class="form-control required"></p>';
            				}
            							$str .= '</div>';
            			
            					$str .= '</div>';
            					$num++;
            				}
            				echo $str;
							?>
							<div class="form-group">
								<div class="col-lg-10 col-lg-offset-2">
									<button type="submit" class="btn btn-primary">ยืนยัน</button>
									<button class="btn btn-default">ยกเลิก</button>
								</div>
							</div>
							<?php
            				}
							else
							{
							echo "ไม่พบข้อมูลแบบทดสอบ";
							}
            				?>
            			            		
            			            		
            <?php 			            		
            		}
            		else
            		{
            			echo "คุณเคยทำแบบทดสอบนี้แล้ว";
            		}

            		
            	}
            }
            else
            {
            	echo "<center>กรุณาใส่ข้อมูลรหัสนักศึกษา  <a href='".$base_url."'>หน้าแรก</a></center>";
            }
            
            ?>
            

            </form>
            </div>
        </div>
      </div>
      <footer>
        <div class="row">
          <div class="col-lg-12">
            <ul class="list-unstyled">
              <li class="pull-right"><a href="#top">Back to top</a></li>
            </ul>
          </div>
        </div>
      </footer>
    </div>
  </body>
</html>
<script type="text/javascript">  
    floatingMenu.add('floatdiv',  
        {  
            // Represents distance from left or right browser window  
            // border depending upon property used. Only one should be  
            // specified.  
            // targetLeft: 0,  
            targetRight: 10,  
  
            // Represents distance from top or bottom browser window  
            // border depending upon property used. Only one should be  
            // specified.  
           // targetTop: 10,  
            //targetBottom: 0,  
            targetBottom: 20, 
  
            // Uncomment one of those if you need centering on  
            // X- or Y- axis.  
            // centerX: true,  
            // centerY: true,  
  
            // Remove this one if you don't want snap effect  
            snap: true  
        });  
</script>  
